package com.servlet;

import com.alibaba.fastjson.JSON;
import com.entity.composite.DeveloperInfo;
import com.entity.composite.ModDownloadInfo;
import com.entity.composite.ModInfo;
import com.entity.element_relation.Element;
import com.entity.short_reviews.ModShortReviews;
import com.entity.version_relation.Version;
import com.service.ModService;
import com.service.impl.ModServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 模组
 * @author wang_
 * @version 2024.3.4.1
 * @description
 * @date 2025/7/14
 */
@WebServlet("/ModServlet")
public class ModServlet extends AbstractHttpServlet {
	private final Logger     LOG        = LogManager.getLogger(ModServlet.class);
	
	@Override
	public void doHandle(HttpServletRequest request, HttpServletResponse response, PrintWriter out, HttpSession session) throws IOException, ServletException {
		String modId = request.getParameter("modId");
		String type = request.getParameter("type");
		switch (type) {
			// 获取精选模组
			case "getSelectedMods" -> getSelectedMods(out);
			// 获取模组的基本信息
			case "getBasicInformation" -> getBasicInformation(out, modId);
			// 获取模组开发者
			case "getDeveloper" -> getDeveloper(out, modId);
			// 获取模组元素
			case "getElement" -> getElement(out, modId);
			// 获取模组版本
			case "getVersion" -> getVersion(out, modId);
			// 获取模组短评
			case "getShortReviews" -> getShortReviews(out, modId);
			// 获取模组下载
			case "getDownload" -> getDownload(out, modId);
		}
	}
	
	private void getSelectedMods(PrintWriter out) {
		ModService service = new ModServiceImpl();
		List<ModInfo> selectedMods = service.getSelectedMods();
		String json = JSON.toJSONString(selectedMods);
		out.print(json);
	}
	
	private void getBasicInformation(PrintWriter out, String modId) {
		if (modId == null) {
			return;
		}
		ModService service = new ModServiceImpl();
		ModInfo modInfo = service.getBasicInformation(Integer.parseInt(modId));
		String json = JSON.toJSONString(modInfo);
		out.print(json);
	}
	
	private void getDeveloper(PrintWriter out, String modId) {
		if (modId == null) {
			return;
		}
		ModService service = new ModServiceImpl();
		List<DeveloperInfo> modInfo = service.getDeveloper(Integer.parseInt(modId));
		String json = JSON.toJSONString(modInfo);
		out.print(json);
	}
	
	private void getElement(PrintWriter out, String modId) {
		if (modId == null) {
			return;
		}
		ModService service = new ModServiceImpl();
		List<Element> modInfo = service.getElement(Integer.parseInt(modId));
		String json = JSON.toJSONString(modInfo);
		out.print(json);
	}
	
	private void getVersion(PrintWriter out, String modId) {
		if (modId == null) {
			return;
		}
		ModService service = new ModServiceImpl();
		List<Version> modInfo = service.getVersion(Integer.parseInt(modId));
		String json = JSON.toJSONString(modInfo);
		out.print(json);
	}
	
	private void getShortReviews(PrintWriter out, String modId) {
		if (modId == null) {
			return;
		}
		ModService service = new ModServiceImpl();
		List<ModShortReviews> modInfo = service.getShortReviews(Integer.parseInt(modId));
		String json = JSON.toJSONString(modInfo);
		out.print(json);
	}
	
	private void getDownload(PrintWriter out, String modId) {
		if (modId == null) {
			return;
		}
		ModService service = new ModServiceImpl();
		List<ModDownloadInfo> modInfo = service.getDownload(Integer.parseInt(modId));
		String json = JSON.toJSONString(modInfo);
		out.print(json);
	}
}
